package com.qf.blogsystem.dao.impl;

import com.qf.blogsystem.core.utils.DruidUtils;
import com.qf.blogsystem.dao.UserDao;
import com.qf.blogsystem.entity.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author Crazy
 * @version 1.0
 * @description: 用户Dao实现类
 * @date 2022/12/6 0:32
 */
public class UserDaoImpl implements UserDao {
    /**
     * 创建DButils执行对象
     */
    private QueryRunner queryRunner=new QueryRunner(DruidUtils.getDataSource());
    /**
     * 添加用户信息
     * @param user
     * @return Integer
     * @throws Exception
     */
    @Override
    public Integer insert(User user) throws Exception {
        String sql = "insert into t_user(username,password,email,type,status,activationCode,createTime) values(?,?,?,?,?,?,?)";
        return queryRunner.update(sql, user.getUsername(), user.getPassword(), user.getEmail(), user.getType(), user.getStatus(), user.getActivationCode(), user.getCreateTime());
    }

    /**
     * 根据用户id查询用户信息
     * @param id
     * @return User
     * @throws Exception
     */
    @Override
    public User selectById(Integer id) throws Exception {
        String sql = "select * from t_user where id = ?";
        return queryRunner.query(sql,new BeanHandler<User>(User.class),id);
    }

    /**
     * 根据用户id修改用户信息
     * @param user
     * @return Integer
     * @throws Exception
     */
    @Override
    public Integer updateById(User user) throws Exception {
        String sql = "update t_user set username = ?,email = ?,status = ? where id = ?";
        return queryRunner.update(sql, user.getUsername(), user.getEmail(), user.getStatus(), user.getId());
    }

    @Override
    public Integer deleteById(Integer id) throws Exception {
        return null;
    }

    @Override
    public List<User> selectList() throws Exception {
        return null;
    }

    @Override
    public Integer batchDelete(List<Integer> ids) throws Exception {
        return null;
    }

    /**
     * 根据激活码查询用户信息
     * @param activationCode
     * @return User
     * @throws Exception
     */
    @Override
    public User selectByActivationCode(String activationCode) throws Exception {
        String sql = "select * from t_user where activationCode = ?";
        return queryRunner.query(sql, new BeanHandler<User>(User.class), activationCode);
    }

    /**
     * 根据用户名查询用户信息
     * @param username
     * @return User
     * @throws Exception
     */
    @Override
    public User selectByUsername(String username) throws Exception {
        String sql = "select * from t_user where username = ?";
        return queryRunner.query(sql, new BeanHandler<User>(User.class), username);
    }

    /**
     * 根据用户id修改用户头像信息
     * @param id
     * @param filePath
     * @throws Exception
     */
    @Override
    public Integer updateHeaderUrl(Integer id, String filePath) throws Exception {
        String sql = "update t_user set headerUrl = ? where id = ?";
        return queryRunner.update(sql, filePath, id);
    }

    /**
     * 根据邮箱查询用户信息
     * @param email
     * @return User
     */
    @Override
    public User selectByEmail(String email) throws Exception {
        String sql = "select * from t_user where email = ?";
        return queryRunner.query(sql, new BeanHandler<User>(User.class), email);
    }
}
